package com.dyz.swordFingerOffer;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: daiyizheng
 * @date: 2021/5/30 16:57
 * @description:
 */
public class deleteDuplication18 {
    //去重
    public ListNode18 deleteDuplication(ListNode18 pHead) {
        ListNode18 root = new ListNode18(pHead.val);
        ListNode18 body = root;
        ListNode18 cur = pHead.next;
        while (cur!=null){
            if(body.val==cur.val){
                cur = cur.next;
            }else {
                body.next = new ListNode18(cur.val);
                cur = cur.next;
                body = body.next;
            }
        }
        return root;
    }
    // 多的全部删除
    public ListNode18 deleteDuplication1(ListNode18 pHead){
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        ListNode18 cur = pHead;
        ListNode18 root = null;
        ListNode18 body = root;
        while (cur!=null){
            if(map.get(cur.val)!=null && map.get(cur.val)!=0){
                map.put(cur.val, map.get(cur.val)+1);
            }else {
                map.put(cur.val, 1);
            }
            cur = cur.next;
        }
        ListNode18 cur1 = pHead;
        while (cur1!=null){
            if(map.get(cur1.val)>1){
                cur1 = cur1.next;
            }else {
                if(root!=null){
                    body.next = new ListNode18(cur1.val);
                    body = body.next;
                }else {
                    root = new ListNode18(cur1.val);
                }
                cur1 = cur1.next;
            }
        }
        return root;
    }
}

class ListNode18 {
    int val;
    ListNode18 next = null;

    ListNode18(int val) {
        this.val = val;
    }
}
